<style lang="less">
    @import './login.less';
</style>

<template>


    <div class="login" @keydown.enter="handleSubmit">

        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                            <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                            <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long :loading="loading">
                                <span v-if="!loading">登录</span>
                                <span v-else>登录中...</span>
                            </Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip">输入任意用户名和密码即可</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
    import Cookies from 'js-cookie';
    import Util from '../libs/util';
    import CryptoJS from "crypto-js";
    var qs = require('qs');
    //登录
    const requestLogin = params => {
        let words = CryptoJS.enc.Utf8.parse(params.password);
        let base64 = CryptoJS.enc.Base64.stringify(words);
        params.password = base64;
        params.code = '123';
        return Util.axios.post('/ms/login', qs.stringify(params));
    };
    export default {
        data() {
            return {
                loading: false,
                form: {
                    userName: 'admin',
                    password: '123123'
                },
                rules: {
                    userName: [
                        {required: true, message: '账号不能为空', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码不能为空', trigger: 'blur'}
                    ]
                },
                menuData: null
            };
        },
        methods: {
            handleSubmit() {
                var me = this;
                me.loading = true;
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        //1. 登录
                        let loginParams = {userId: this.form.userName, password: this.form.password};
                        requestLogin(loginParams).then(res => {
                            me.loading = false;
                            var data = res.data;
                            if (data.result) {
                                Cookies.set('user', this.form.userName);
                                this.$store.commit('setAvator', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80');
                                //登录成功跳转
                                this.$router.push({
                                    name: 'home_index'
                                });
                            }
                            else {
                                this.$Notice.error({
                                    title: '提示',
                                    desc: data.msg
                                });
                            }

                        });

//                    Cookies.set('user', this.form.userName);
//                    Cookies.set('password', this.form.password);
//                    this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
//                    if (this.form.userName === 'iview_admin') {
//                        Cookies.set('access', 0);
//                    } else {
//                        Cookies.set('access', 1);
//                    }
//                    this.$router.push({
//                        name: 'home_index'
//                    });
                    }
                });
            }
        }
    };
</script>

<style>

</style>
